package net.ib.rpc.channel;

import net.ib.mn.protocol.CommonEnvelope.EnvelopeType;
import net.ib.rpc.message.RpcRequestInfo;
import net.ib.rpc.message.RpcResponseInfo;

import org.jboss.netty.channel.Channel;

import com.google.protobuf.Descriptors.MethodDescriptor;
import com.google.protobuf.Message;
import com.google.protobuf.RpcCallback;
import com.google.protobuf.RpcChannel;
import com.google.protobuf.RpcController;

public class RpcChannelImpl implements RpcChannel {

	private Channel channel;

	public RpcChannelImpl(Channel channel) {
		this.channel = channel;
	}

	@Override
	public void callMethod(MethodDescriptor method, RpcController controller,
			Message request, Message responsePrototype,
			RpcCallback<Message> done) {

		if (!channel.isConnected()) {
			return;
		}

		RpcRequestInfo requestInfo = new RpcRequestInfo(request,
				EnvelopeType.REQUEST, method, new RpcResponseInfo(done,
						responsePrototype), 0, 0, Long.MAX_VALUE);

		channel.write(requestInfo);
	}
}
